<?php
class PublicAction extends Action {
	public function login () {
		if($_POST) {
			$formdata = array();
			$formdata['username'] = $_POST['username'];
			$formdata['password'] = $_POST['password'];
			$formdata['verify'] = $_POST['verify'];
			$Manager = D('Manager');
			if($Manager->create()) {
				// 超级管理员权限
				if($formdata['username'] == C('SUPERADMIN')[0] && $formdata['password'] == C('SUPERADMIN')[1]) {
					$_SESSION[C("USER_AUTH_KEY")] = 'ADMINISTRATOR';
	            	$_SESSION['NAME'] = '超级管理员';
					$this->ajaxReturn(1, '登陆成功，即将跳转', 1);
				}

				//通过用户名获取用户信息
				$managerData = $Manager->getByUsername($formdata['username']);

				// 判断账号是否存在
				if(count($managerData) == 0) {
					$this->ajaxReturn(0, '用户名' . $formdata['username'] . '不存在！', 0);
				} else {
					$managerData = $managerData[0];
				}

				// 判断密码正确性
				if(md5($formdata['password'] . $managerData['secretkey']) !== $managerData['password']) {
					$this->ajaxReturn(0, '密码错误！', 0);
				}

				//判断账号是否启用
				if($managerData['enable'] === '0') {
					$this->ajaxReturn(0, '您的账号没有访问权限！', 0);
				}

				//判断角色是否启用
				$Role = M('Role');
				$roleData = $Role->where('id='.$managerData['role_id'])->select();
				if($roleData[0]['enable'] === '0') {
					$this->ajaxReturn(0, '您所在的角色组没有访问权限！', 0);
				}

				// 用户数据更新操作
				$managerData['last_login_time'] = time();
				$managerData['last_login_ip'] = get_client_ip();
				$managerData['login_count']++;

				if($Manager->save($managerData)) {
					// 证明提交数据正确，配置相关session
					$_SESSION[C("USER_AUTH_KEY")] = $managerData['id'];
	            	$_SESSION['NAME'] = $managerData['username'];
	            	$_SESSION['ROLE'] = $managerData['role_id'];
	            	$_SESSION['TIME'] = time();

	            	// 读取所有权限节点(待开发。。。)
	            	// $RoleSet = D('RoleSet');
	            	// $MethodList = $RoleSet->getAllMethodsByRoleId($_SESSION['ROLE']);
	            	// $MethodHash = array();
	            	// for($i = 0; $i < count($MethodList); $i++) {
	            	// 	$MethodHash[$MethodList[$i]['identifier']] = 1;
	            	// }
	            	// $_SESSION['METHODHASH'] = $MethodHash;

					$this->ajaxReturn(1, '登陆成功，即将跳转', 1);
				} else {
					$this->ajaxReturn(0, $Manager->getError(), 0);
				}
			} else {
				// 字段自动校验返回错误
				$this->ajaxReturn(0, $Manager->getError(), 0);
			}
		} else {
			if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
	            $this->display();
	        } else {
	            $this->redirect('Index/index');
	        }
		}
	}

	// 退出方法
	public function loginout() {
		if(isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION['METHODHASH']);
			$this->redirect(C('USER_AUTH_GATEWAY'));
		}
	}

	public function verify () {
		import('ORG.Util.Image');
		Image::buildImageVerify();
	}

	// 功能设置和节点设置的公用入口页
	public function nodeSetEntry () {
		$module = $_GET['module'];
		$action = $_GET['action'];

		$this->assign('module', $module);
		$this->assign('action', $action);
		$this->display();
	}

	// 供功能设置和模块管理调用的左侧菜单树
	public function nodeSetLeft () {
		$module = $_GET['module'];
		$action = $_GET['action'];

		$Node = M('Node');
		$list = $Node->query("select * from __TABLE__ order by sort");
		
		for($i = 0; $i < count($list); $i++) {
			$list[$i]['url'] = $module . '/' . $action . "?nid=" . $list[$i]['id'];
		}
		$this->assign('list', $list);
		$this->display();
	}
}